using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Dal.Helper;
using EP.Base.BaseService.Manager.Interface.Data;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Dal
{    
	/// <summary>
    /// IndexDeleteLog 
    /// </summary>
    public class IndexDeleteLogs:IIndexDeleteLogs
    {

		/// <summary>
		/// 添加IndexDeleteLog
		/// </summary>
		/// <param name="indexdeletelog"></param>
		/// <returns></returns>
		public int Add(IndexDeleteLog indexdeletelog)
		{
			string sql = @"INSERT INTO [IndexDeleteLog]
			([IndexName],[State],[DeleteTime],[CreateTime],[IndexConfigId],[IndexCreateTime])
			VALUES
			(@IndexName,@State,@DeleteTime,@CreateTime,@IndexConfigId,@IndexCreateTime) 
			SELECT SCOPE_IDENTITY()
			";
			var par = new DynamicParameters();
			par.Add("@IndexName",indexdeletelog.IndexName , DbType.AnsiString);
			par.Add("@State",indexdeletelog.State , DbType.Int16);
			par.Add("@DeleteTime",indexdeletelog.DeleteTime , DbType.DateTime);
            par.Add("@CreateTime", indexdeletelog.CreateTime, DbType.DateTime);
            par.Add("@IndexCreateTime", indexdeletelog.IndexCreateTime, DbType.DateTime);
            par.Add("@IndexConfigId", indexdeletelog.IndexConfigId, DbType.Int32);
			return DapWrapper.InnerQueryScalarSql<int>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
        /// <summary>
        /// 添加IndexDeleteLog如果索引存在不做任何操作
        /// </summary>
        /// <param name="indexdeletelog"></param>
        /// <returns></returns>
        public int AddByNoIndex(IndexDeleteLog indexdeletelog)
        {
            string sql = @"Pro_IndexDeleteLogAdd";
            var par = new DynamicParameters();
            par.Add("@IndexName", indexdeletelog.IndexName, DbType.AnsiString);
            par.Add("@State", indexdeletelog.State, DbType.Int16);
            par.Add("@IndexCreateTime", indexdeletelog.IndexCreateTime, DbType.DateTime);
            par.Add("@CreateTime", indexdeletelog.CreateTime, DbType.DateTime);
            par.Add("@IndexConfigId", indexdeletelog.IndexConfigId, DbType.Int32);
            return DapWrapper.InnerQueryScalarProc<int>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
        }
		/// <summary>
		/// 修改IndexDeleteLog
		/// </summary>
		/// <param name="indexdeletelog"></param>
		/// <returns></returns>
		public int Update(IndexDeleteLog indexdeletelog)
		{
			string sql = @"UPDATE  [IndexDeleteLog] SET 
						IndexName=@IndexName,
						State=@State,
						DeleteTime=@DeleteTime,
						CreateTime=@CreateTime
 WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", indexdeletelog.Id, DbType.Int32);
			par.Add("@IndexName",indexdeletelog.IndexName , DbType.AnsiString);
			par.Add("@State",indexdeletelog.State , DbType.Int16);
			par.Add("@DeleteTime",indexdeletelog.DeleteTime , DbType.DateTime);
			par.Add("@CreateTime",indexdeletelog.CreateTime , DbType.DateTime);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 根据id获取IndexDeleteLog
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public IndexDeleteLog Get(int id)
		{
            string sql = "select [Id],[IndexName],[State],[DeleteTime],[CreateTime],[IndexConfigId],[IndexCreateTime] FROM [IndexDeleteLog] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int32);
			return DapWrapper.InnerQuerySql<IndexDeleteLog>(DbConfig.EpBaseServiceManagerConnectionString, sql, par).FirstOrDefault();
		}
		/// <summary>
		/// 根据id删除IndexDeleteLog
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public Int32 Delete(int id)
		{
			string sql="DELETE [IndexDeleteLog] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int32);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 根据ids删除IndexDeleteLog多条记录
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		public Int32 Deletes(int[] ids)
		{
			if (ids.Length == 0)
                return 0;
			string sql="DELETE [IndexDeleteLog] WHERE Id IN ("+string.Join(",",ids)+")";
			return DapWrapper.InnerExecuteText(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
		/// <summary>
       /// 获取IndexDeleteLog分页列表
       /// </summary>
       /// <param name="pageIndex">页码</param>
       /// <param name="pageSize">每页显示条数</param>
       /// <param name="beginTime">开始时间</param>
       /// <param name="endTime">结束时间</param>
       /// <param name="recordCount">总记录数</param>
       /// <returns>IndexDeleteLog列表</returns>
       public List<IndexDeleteLog> GetPageList(int pageIndex, int pageSize, DateTime? beginTime, DateTime? endTime, out int recordCount)
       {
           recordCount = 0;
           var par = new DynamicParameters();
           par.Add("@PageIndex", pageIndex, DbType.Int32);
           par.Add("@PageSize", pageSize, DbType.Int32);
           par.Add("@BeginTime", beginTime, DbType.DateTime);
           par.Add("@EndTime", !endTime.HasValue ? endTime : endTime.Value.AddDays(1).AddMilliseconds(-1), DbType.DateTime);
           par.Add("@TotalCount", recordCount, DbType.Int32, ParameterDirection.Output);
           var result = DapWrapper.InnerQueryProc<IndexDeleteLog>(DbConfig.EpBaseServiceManagerConnectionString, "proc_GetIndexDeleteLogPageList", par);
           recordCount = par.Get<int>("@TotalCount");
           return result;
       }
	   /// <summary>
		/// 获取所有IndexDeleteLog
		/// </summary>
		/// <returns></returns>
		public List<IndexDeleteLog> GetAll()
		{
            string sql = "select [Id],[IndexName],[State],[DeleteTime],[CreateTime],[IndexConfigId],[IndexCreateTime] FROM [IndexDeleteLog]";
			
			return DapWrapper.InnerQuerySql<IndexDeleteLog>(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
        /// <summary>
        /// 根据EsTaskConfigId获取所有IndexDeleteLog
        /// </summary>
        /// <returns></returns>
        public List<IndexDeleteLog> GetByIndexConfigId(int indexConfigId,short state)
	    {
            string sql = "select [Id],[IndexName],[State],[DeleteTime],[CreateTime],[IndexConfigId],[IndexCreateTime] FROM [IndexDeleteLog] WHERE State=@State AND IndexConfigId=@IndexConfigId ORDER BY [IndexCreateTime]";
            var par = new DynamicParameters();
            par.Add("@IndexConfigId", indexConfigId, DbType.Int32);
            par.Add("@State", state, DbType.Int16);
            return DapWrapper.InnerQuerySql<IndexDeleteLog>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
	    }

	    /// <summary>
	    /// 
	    /// </summary>
	    /// <param name="indexConfigId"></param>
	    /// <param name="oldState"></param>
	    /// <param name="newState"></param>
	    /// <returns></returns>
	    public bool UpdateStateByIndexConfigId(int indexConfigId, short oldState,short newState)
	    {
            string sql = "UPDATE [IndexDeleteLog] SET State=@NewState  WHERE State=@OldState AND IndexConfigId=@IndexConfigId";
            var par = new DynamicParameters();
            par.Add("@IndexConfigId", indexConfigId, DbType.Int32);
            par.Add("@OldState", oldState, DbType.Int16);
            par.Add("@NewState", newState, DbType.Int16);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par)>0;
	    }

	    public bool UpdateStateAndDeleteTimeById(int id,short state, DateTime deleteTime)
	    {
            string sql = "UPDATE [IndexDeleteLog] SET State=@State,DeleteTime=@DeleteTime  WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@DeleteTime", deleteTime, DbType.DateTime);
            par.Add("@Id", id, DbType.Int32);
            par.Add("@State", state, DbType.Int16);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par) > 0;
	    }
    }
}
